/**
 * 车辆基本信息
 *
 */
var express = require('express');
var request = require('request');

var url = require('url');
var qstr = require("querystring");
var fs = require("fs");
var pro = require('../../property/cuspro');
var auth = require('../../auth/auth');
var util = require('../../util/commonutil');
var router = express.Router();
// var querystring = require('querystring');
var cache = require('memory-cache');
//dept
router.post('/dept', function (req, res, next) {

    var pathname = url.parse(req.url).pathname;  //pathname => list

    // var arg = url.parse(req.url).query;          //arg => name=a&id=5
    // console.log("Request for " + arg );
    // var str = qstr.parse(arg);                //str=> {name:'a',id:'5'}

    var arg = url.parse(req.url, true).query;   //arg1 => {name:'a',id:'5'}
    // console.log("Request for " + arg1 );

    // var name = qstr.parse(arg).draw;         //name => a
    // console.log("name = "+name);
    var query = req.query;//参数
    var sid = auth.getSessionID(req, res, next);
    var qs = req.body;
    arg.sid = sid;
    var data = [
        {id:1, parentId:0, name: "父节点1"},
        {id:11, parentId:1, name: "子节点1"},
        {id:12, parentId:1, name: "子节点2"}
    ];
    //res.json(data);
    //req.params，req.query 和 req.body 中的表单数据
    request({
        url: pro.serveraddr + '/sysDept/loadZTree',
        method: "GET",
        qs: arg
    }, function (error, response, body) {
        if (!error && response.statusCode == 200) {
            var data = JSON.parse(body);
            if (data) {
                res.json(data);
            } else {
                res.json("");
            }
        }
    });
});
router.post('/deptCar', function (req, res, next) {
    auth.checkAuth(req, res, next);
    var pathname = url.parse(req.url).pathname;  //pathname => list
    var arg = url.parse(req.url, true).query;   //arg1 => {name:'a',id:'5'}
    var query = req.query;//参数
    var sid = auth.getSessionID(req, res, next);
    var qs = req.body;
    arg.sid = sid;
    var data = [
        {id:1, parentId:0, name: "部门-01",parentName:"所有部门",code:"100",memo:"memo",isCar:false,warningType:"999",iconSkin:""},
        {id:11, parentId:1, name: "车辆-01",parentName:"部门-01",code:"101",memo:"memo",isCar:true,warningType:"0",iconSkin:"btnImage iconArea offlineIcon"},
        {id:12, parentId:1, name: "车辆-02",parentName:"部门-02",code:"102",memo:"memo",isCar:true,warningType:"1",iconSkin:"btnImage iconArea onlineDriving"}
    ];
    //res.json(data);
    // req.params，req.query 和 req.body 中的表单数据
    request({
        url: pro.serveraddr + '/baseCar/loadZTree',
        method: "GET",
        qs: arg
    }, function (error, response, body) {
        console.log("车辆树信息>>"+body);
        if (!error && response.statusCode == 200) {
            try{
                var data = JSON.parse(body);

                if (data) {
                    var midData = [];
                    if(data.length){
                        for(var i=0;i<data.length;i++){
                            data[i]["iconSkin"]="";
                            midData.push(data[i]);
                        }
                    }
                    cache.put("deptCarData",midData);
                    res.json(midData);
                } else {
                    var __deptCar =cache.get("deptCarData");
                    res.json(__deptCar);
                }
            }catch (e) {
                console.log(e.message);
                var __deptCar =cache.get("deptCarData");
                res.json(__deptCar);
            }
        }
    });
});
router.post('/fences', function (req, res, next) {
    auth.checkAuth(req, res, next);
    var pathname = url.parse(req.url).pathname;  //pathname => list
    var arg = url.parse(req.url, true).query;   //arg1 => {name:'a',id:'5'}
    var query = req.query;//参数
    // req.params，req.query 和 req.body 中的表单数据
    request({
        url: pro.serveraddr + '/baseCommonFence/loadZTree',
        method: "GET",
        qs: arg
    }, function (error, response, body) {
        if (!error && response.statusCode == 200) {
            var data = JSON.parse(body);
            if (data) {
                res.json(data);
            } else {
                res.json("");
            }
        }
    });
});
module.exports = router;
